package com.mixsmart.spellsql.where.exp;

import com.mixsmart.spellsql.IExpression;
import com.mixsmart.spellsql.where.CompositeWhere;
import com.mixsmart.utils.StringUtils;

/**
 * 直接赋值表达式
 * @author lmq  2017年12月27日
 * @version 1.0
 * @since JDK1.7
 */
public class AssignmentExpression extends CompositeWhere implements IExpression {

    private String fieldName;
    
    private Object value;
    
    public AssignmentExpression(String fieldName, Object value) {
        this.fieldName = fieldName;
        this.value = value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public String build() {
        String cnd = fieldName+"=";
        if(value instanceof Integer) {
            cnd += Integer.parseInt(value.toString());
        } else if(value instanceof Double) {
            cnd += Double.parseDouble(value.toString());
        } else if(value instanceof Float) {
            cnd += Float.parseFloat(value.toString());
        } else if(value instanceof String) {
            cnd += "'" + value.toString() + "'";
        } else {
            cnd += "'" + StringUtils.handleNull(value.toString()) + "'";
        }
        return cnd;
    }
    
}
